package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.TokenService;
import com.yixun.qfbao.apidto.GetTokenReqDto;
import com.yixun.qfbao.apidto.GetTokenResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Token;
import com.yixun.qfbao.utils.TokenUtils;
import com.yixun.qfbao.vo.GetTokenReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("GetTokenHander")
public class GetTokenHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetTokenHander.class);

    @Autowired
    TokenService tokenService;
    @Autowired
    TokenUtils tokenUtils;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<GetTokenResDto> res = new BaseResponseDTO<GetTokenResDto>();
        GetTokenResDto resDto = new GetTokenResDto();
        GetTokenReqVo reqVo = (GetTokenReqVo)reqDto;
        GetTokenReqDto getTokenReqDto = reqVo.getParams();
        try {
            Token token = tokenService.createToken(getTokenReqDto);
            if(token == null){
                res.setResult(ResultEnum.FAIL.getNo());
                res.setErrorMsg("未查询到该公司uuid");
                return res;
            }
            //缓存token
            tokenUtils.put(token.getStrToken(),token);

            resDto.setToken(token.getStrToken());
            // 有效时间10分钟
            resDto.setTimestamp(108000L);
        } catch (Exception e) {
            logger.error("GetTokenHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取token失败");
        }
        res.setResult(ResultEnum.SUCCESS.getNo());
        res.setResData(resDto);
        return res;
    }
}
